/*
 *   Copyright 2011-2012 Hai Bison
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

/*
 * PanelPreferencesAutoUpdate.java
 *
 * Created on Sep 24, 2011, 12:29:47 PM
 */
package group.pals.mailgate.preferences.ui;

import group.pals.mailgate.ui.IPreferences;
import group.pals.mailgate.preferences.Preferences;
import group.pals.mailgate.utils.Utils;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

/**
 *
 * @author Hai Bison
 */
public class PanelPreferencesAutoUpdate extends javax.swing.JPanel implements IPreferences {

    private final Preferences _Preferences;
    private final Utils _Utils;

    /** Creates new form PanelPreferencesAutoUpdate */
    public PanelPreferencesAutoUpdate() {
        initComponents();

        this._Preferences = Preferences.getInstance();
        this._Utils = Utils.getInstance();

        initActions();
    }

    private void initActions() {
        actionCheckUpdatesNow = new ActionCheckUpdatesNow();
        buttonCheckUpdatesNow.setAction(actionCheckUpdatesNow);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        chkAutoUpdate = new javax.swing.JCheckBox();
        buttonCheckUpdatesNow = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Auto update:"));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        chkAutoUpdate.setText("Auto update");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(chkAutoUpdate, gridBagConstraints);

        buttonCheckUpdatesNow.setText("jButton1");
        jPanel1.add(buttonCheckUpdatesNow, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCheckUpdatesNow;
    private javax.swing.JCheckBox chkAutoUpdate;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    /*----------------------------------------------------------------------------
     * IPreferences
     */
    public void loadPreferences() {
        chkAutoUpdate.setSelected(_Preferences.isAutoUpdateOn());
    }

    public boolean storePreferences(boolean prepare) {
        _Preferences.setAutoUpdateOn(chkAutoUpdate.isSelected());
        return true;
    }

    public void closeResources() {
        //do nothing
    }

    /*----------------------------------------------------------------------------
     * ActionCheckUpdatesNow
     */
    private ActionCheckUpdatesNow actionCheckUpdatesNow;

    private class ActionCheckUpdatesNow extends AbstractAction {

        public ActionCheckUpdatesNow() {
            super("Check now!");
        }

        public void actionPerformed(ActionEvent e) {
            _Utils.forceUpdaterUpdate();
        }
    }
}
